<?php
namespace app\yunda\home;

use app\yunda\model\Active as ActiveModel;
use app\yunda\model\User;
use app\yunda\model\Score_order;
use think\Db;
/**
 * 我的活动
 * @package app\yunda\home
 */
class Active extends Base {
    // 获取列表
    public function index(){
        $param=request()->only('openid');
        // 查询字段
        $map = $this->getMap();
        $map['status']=1;
        $Post = new ActiveModel();
        $data =  $Post
            ->order('sort desc,id desc')
            ->where($map)
            ->paginate()
            ->each(function($item){
                //对item增加额外字段
                $item['icon']=get_images_all($item['icon']);
                return $item;
            });
        $data = $data->toArray();
        //获取用户剩余抽奖次数
        $user=User::where('openid',$param['openid'])->field('award_time')->find();
        return mogo_json($data,'',$user);
    }

    //抽奖
    public function luck_draw()
    {
        $data=request()->only('openid');
        if(!isset($data['openid'])||empty($data['openid'])) return mogo_error('参数错误',404);

        //获取用户抽奖次数
        $row=User::get(['openid'=>$data['openid']]);
        if(empty($row))return mogo_error('用户不存在',401);
        if($row['award_time']<=0) return mogo_error('抽奖次数不足',0);

        //判断用户是否填写个人信息邮编，地区，地址，如没有则需设置
        if(empty($row['post_code']||$row['home']||$row['address']))
            return mogo_error('请您去完善个人信息',2);

        //判断每个用户的抽奖次数是否超过设定次数
        $num=Db::table('dp_yd_score_order')
            ->where(['openid'=>$data['openid'],'order_type'=>2])
            ->whereTime('create_time','d')->count();
        //if($num>=config('every_day_award_time'))return mogo_error('您今日抽奖次数已达上限',0);
        //抽奖
        $res=ActiveModel::doLuck();

        //halt($res);
        //判断该奖品库存是否足够
        $res['btc']=0;
        if(!in_array($res['yes']['id'],config('cy_award_id'))){
            $ku=ActiveModel::get($res['yes']['id']);
            if($ku['stock']<=0) return mogo_error('很抱歉，您所抽中的奖项已经中完！',404);
            $res['btc']=1;
        }
        //扣除抽奖次数
        $data_2=[
            'id'=>$row['id'],
            'award_time'=>$row['award_time']-1,
        ];
        User::update($data_2);

        //存入中奖表
        $data_1=[
            'openid'=>$data['openid'],
            'good_id'=>$res['yes']['id'],
            'order_type'=>2,
        ];
        $b=Score_order::create($data_1);

        //扣除奖品库存
        if(!in_array($res['yes']['id'],config('cy_award_id'))) {
            $data_3 = ['id' => $res['yes']['id'], 'stock' => $ku['stock'] - 1];
            ActiveModel::update($data_3);
        }

        //返回抽奖结果
        if(!$b){
            mogo_error('系统繁忙',404);
        }
        $jiang=ActiveModel::get($res['yes']['id']);

        return mogo_json($res,'yes中奖物品id,no没中奖id',1);
    }

    //我的奖品
    public function myaward($openid='')
    {
        if(empty($openid))return mogo_error('参数错误',404);
        // 查询字段
        $map = $this->getMap();
        $Address = new Score_order;
        $map['yd_score_order.delete_time']=NULL;
        $map['yd_score_order.order_type']=2;//抽奖记录
        $map['yd_score_order.openid']=$openid;
        $map['yd_score_order.good_id']=['notin',config('cy_award_id')];
        // 自定义where查询
        $data = Db::view('yd_score_order',true)
            ->view('yd_user',['name','post_code','home','address','phone'],'yd_user.openid=yd_score_order.openid','left')
            ->view('yd_active','active_title,icon','yd_active.id=yd_score_order.good_id','left')
            ->where($map)
            ->order('create_time desc')
            ->paginate()->each(function($item){
                $item['address1']=$item['home'].$item['address'];
                $arr=['未兑换','已兑换'];
                $item['is_pay']=$arr[$item['is_pay']];
                $item['create_time']=format_time($item['create_time']);
                $item['icon']=get_images_all($item['icon']);
                return $item;
            });
        $data = $data->toArray();
        return mogo_json($data,'',1);
    }

}